package com.fastloan.jn.mgt.product.controller;

import com.fastloan.jn.mgt.product.model.JnProduct;
import com.fastloan.jn.mgt.product.service.JnProductService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author chenkai
 *         createTime 2017-10-2017/10/20 14:29
 */
@RestController
@RequestMapping("/api/web/product")
public class JnProductController extends BaseController<JnProduct> {
    @Autowired
    private JnProductService productService;

    /**
     * 产品分页
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList() {
        return dataPage();
    }

    /**
     * 根据id查询产品信息
     *
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public ResultTo get(@PathVariable String productId) {
        return new ResultTo().setData(productService.selectByPrimaryKey(productId));
    }

    /**
     * 新增产品
     *
     * @param product
     * @return
     */
    @PostMapping
    public ResultTo insert(JnProduct product) {
        product.setCreateTime(new Date());
        return productService.insertSelective(product) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    /**
     * 更新产品信息
     *
     * @param product
     * @return
     */
    @PutMapping
    public ResultTo update(JnProduct product) {
        try {
            return productService.updateProduct(product);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 产品发布
     *
     * @param productId
     * @return
     */
    @PutMapping("/approve/{productId}")
    public ResultTo approve(@PathVariable String productId) {
        try {
            return productService.approve(productId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 删除产品
     *
     * @param productId
     * @return
     */
    @DeleteMapping("/{productId}")
    public ResultTo deleteProduct(@PathVariable String productId) {
        try {
            return productService.deleteProduct(productId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

}
